package com.hhsjdesign.dynamicui.utils

import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import org.json.JSONObject
import java.lang.reflect.Constructor

//数据解析器，解析数据为WidgetData
object WidgetDataParser {

    @JvmStatic
    fun parseWidget(jo: JSONObject, parentType: String?): WidgetData? {
        val widgetType: String = jo.optString("type")!!
        //1，创建组件模型，和组件特殊属性
        var clazz: Class<out WidgetData>? =
            ClassCachePool.getClassByWidgetType(ClassCachePool.WIDGET_DATA, widgetType)
                    as? Class<out WidgetData>

        var constructor: Constructor<out WidgetData>? =
            clazz?.getDeclaredConstructor(JSONObject::class.java)
        val widgetData = constructor?.newInstance(jo)
        widgetData?.parentLayoutType = parentType
        return widgetData ?: WidgetData(jo).apply {
            this.parentLayoutType = parentType
        }
    }

}